﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace RapChieuPhim.Modules
{
    public partial class QL_Room : UserControl
    {
        private int _MaRap = Login.user.NhanVien.Rap.MaRap;
        //private int _MaRap = 2;

        public QL_Room()
        {
            InitializeComponent();
        }

        private void LoadData()
        {
            gridControlMain.DataSource = BUS.PhongChieuBUS.GetAllbyMaRap(_MaRap);
        }

        private void QL_Room_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void navBarItem4_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (dxValidationProvider1.Validate())
            {
                int _tempid = BUS.PhongChieuBUS.LayLastPhongChieu() + 1;
                if (XtraMessageBox.Show("Thêm phòng chiếu này ?", null, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DTO.PhongChieuDTO _Phong = new DTO.PhongChieuDTO();
                    _Phong.MaPhong = _tempid;
                    _Phong.SoHangGhe = int.Parse(txtSoHangGhe.Text);
                    _Phong.SoDayGhe = int.Parse(txtSoDayGhe.Text);
                    _Phong.MoTa = txtMoTa.Text;
                    if (radio3D.Checked)
                        _Phong.DKKiThuat = true;
                    else if (radio2D.Checked)
                        _Phong.DKKiThuat = false;
                    if (radioHD.Checked)
                        _Phong.DKKiThuat = true;
                    else if (radioBT.Checked)
                        _Phong.DKKiThuat = false;
                    BUS.PhongChieuBUS.Insert(_Phong, _MaRap);
                    LoadData();
                    gridView1.FocusedRowHandle = gridView1.RowCount - 1;
                    XtraMessageBox.Show("Thêm thành công !", null, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0 || gridView1.FocusedRowHandle >= gridView1.RowCount)
                return;
            int _Value = int.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colMa).ToString());
            DTO.PhongChieuDTO _PhongDTO = BUS.PhongChieuBUS.GetByID(_Value);
            BindingData(_PhongDTO);
        }

        private void BindingData(DTO.PhongChieuDTO _PhongDTO)
        {
            txtMa.Text = _PhongDTO.MaPhong.ToString();
            txtMoTa.Text = _PhongDTO.MoTa;
            txtSoDayGhe.Text = _PhongDTO.SoDayGhe.ToString();
            txtSoHangGhe.Text = _PhongDTO.SoHangGhe.ToString();
            if (_PhongDTO.DKKiThuat)
                radio3D.Checked = true;
            else
                radio2D.Checked = true;
            if (_PhongDTO.TinhTrang)
                radioHD.Checked = true;
            else
                radioBT.Checked = true;
        }

        private void navBarItem5_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (XtraMessageBox.Show("Xóa phòng chiếu này ?", null, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                int _Value = int.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colMa).ToString());
                BUS.PhongChieuBUS.Delete(_Value);
                LoadData();
                XtraMessageBox.Show("Xóa thành công !", null, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void navBarItem6_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (dxValidationProvider1.Validate())
            {
                if (XtraMessageBox.Show("Sửa phòng chiếu này ?", null, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    int _Value = int.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colMa).ToString());
                    DTO.PhongChieuDTO _Phong = new DTO.PhongChieuDTO();
                    _Phong.MaPhong = _Value;
                    _Phong.SoHangGhe = int.Parse(txtSoHangGhe.Text);
                    _Phong.SoDayGhe = int.Parse(txtSoDayGhe.Text);
                    _Phong.MoTa = txtMoTa.Text;
                    if (radio3D.Checked)
                        _Phong.DKKiThuat = true;
                    else if (radio2D.Checked) 
                        _Phong.DKKiThuat = false;
                    if (radioHD.Checked)
                        _Phong.DKKiThuat = true;
                    else if (radioBT.Checked) 
                        _Phong.DKKiThuat = false;
                    BUS.PhongChieuBUS.Update(_Phong, _MaRap);
                    int _temp = gridView1.FocusedRowHandle;
                    LoadData();
                    gridView1.FocusedRowHandle = _temp;
                    XtraMessageBox.Show("Sửa thành công !", null, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void navBarItem7_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            int _temp = gridView1.FocusedRowHandle;
            LoadData();
            gridView1.FocusedRowHandle = _temp;
        }

        private void navBarItem8_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            SaveFileDialog _dialog = new SaveFileDialog();
            _dialog.InitialDirectory = Application.StartupPath.ToString();
            _dialog.Title = "Xuất ra file pdf";
            _dialog.DefaultExt = "pdf";
            _dialog.Filter = "All files (*.*)|*.*";
            _dialog.FilterIndex = 0;
            _dialog.RestoreDirectory = true;
            if (_dialog.ShowDialog() == DialogResult.OK)
            {
                gridControlMain.ExportToPdf(_dialog.FileName.ToString());

                System.Diagnostics.Process _prc = new System.Diagnostics.Process();
                _prc.StartInfo.FileName = System.IO.Path.GetDirectoryName(_dialog.FileName);
                _prc.Start();
            }
        }

        private void navBarItem2_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            SaveFileDialog _dialog = new SaveFileDialog();
            _dialog.InitialDirectory = Application.StartupPath.ToString();
            _dialog.Title = "Xuất ra file pdf";
            _dialog.DefaultExt = "pdf";
            _dialog.Filter = "All files (*.*)|*.*";
            _dialog.FilterIndex = 0;
            _dialog.RestoreDirectory = true;
            if (_dialog.ShowDialog() == DialogResult.OK)
            {
                gridControlMain.ExportToPdf(_dialog.FileName.ToString());

                System.Diagnostics.Process _prc = new System.Diagnostics.Process();
                _prc.StartInfo.FileName = System.IO.Path.GetDirectoryName(_dialog.FileName);
                _prc.Start();
            }
        }
    }
}
